package com.frxuexi.park2303.user

import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.base.entity.MyApplyBean
import com.frxuexi.base.entity.ParkBeanItem
import com.frxuexi.base.entity.PatrolBeanItem
import com.frxuexi.base.entity.RepairBeanItem
import com.frxuexi.base.entity.VisitBeanItem
import com.frxuexi.base.http.Const
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.park2303.databinding.ActivityMyApplyBinding
import com.frxuexi.park2303.user.adapter.MyApplyAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding,UserIntent,UserViewModel>() {
    val list= mutableListOf<MyApplyBean>()
    val myApplyAdapter by lazy { MyApplyAdapter() }

    override fun getVM(): UserViewModel {
        return viewModels<UserViewModel>().value
    }

    override fun getBind(): ActivityMyApplyBinding {
        return ActivityMyApplyBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<ParkBeanItem>
                                items.filter { it.userId==SPUtils.getInstance().getInt(Const.UID) }.forEach {
                                    list.add(MyApplyBean("车位申请","申请${it.areaName} ${it.spaceNumber}车位","2025-07-15 09:00:00",it.spaceStatus))
                                }
                            }
                            RequestType.VISIT->{
                                val items = it.data as Collection<VisitBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("来访申请","访客：${it.visitorName}(1人)|来访时间：${it.appointmentTime.split(" ")[0]}",it.appointmentTime,it.visitStatus))
                                }
                            }
                            RequestType.REPAIR->{
                                val items = it.data as Collection<RepairBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("报修申请","${it.faultDescription} (${it.repairLocation})",it.repairTime,it.processingStatus))
                                }
                            }
                            RequestType.PATROL->{
                                val items = it.data as Collection<PatrolBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("巡更申请","${it.taskName}巡更申请",it.patrolTime,it.patrolStatus))
                                }
                            }

                            else -> {}

                        }
                        myApplyAdapter.data.clear()
                        myApplyAdapter.addData(list)
                        myApplyAdapter.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.headMy.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("我的申请")
            headAdd.visibility=View.GONE
        }

        binding.rvMy.apply {
            adapter=myApplyAdapter
            layoutManager=LinearLayoutManager(this@MyApplyActivity)
        }
        vm.sendIntent(UserIntent.getParkByType)
        vm.sendIntent(UserIntent.getVisitByName(SPUtils.getInstance().getString(Const.UNAME)))
        vm.sendIntent(UserIntent.getRepairByName(SPUtils.getInstance().getString(Const.UNAME)))
        vm.sendIntent(UserIntent.getPatrolByUid(SPUtils.getInstance().getInt(Const.UID)))

        binding.myParkCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="车位申请" })
                notifyDataSetChanged()
            }
        }

        binding.myVisitCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="来访申请" })
                notifyDataSetChanged()
            }
        }
        binding.myRepairCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="报修申请" })
                notifyDataSetChanged()
            }
        }
        binding.myPatrolCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="巡更申请" })
                notifyDataSetChanged()
            }
        }
    }

}